package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.model.FDamHed;
import com.datamation.swadeshi.model.FTranDet;
import com.datamation.swadeshi.model.FTranHed;
import com.datamation.swadeshi.model.OrdHed;
import com.datamation.swadeshi.model.mapping.LoadingMapper;

public class FDamHedDS {
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	//Shared Preferences variables 
	public static final String SETTINGS = "SETTINGS";
	public static SharedPreferences localSP;
	
	public FDamHedDS (Context context)
	{
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException 
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	
	
	@SuppressWarnings("static-access")
	public int createOrUpdateFTranHed(ArrayList<FDamHed>  list) 
	{
		
		int serverdbID =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		
			
			
    for (FDamHed fdamhed : list) 
    {
		
			ContentValues values = new ContentValues();
			
			String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FDAMHED 
					+ " WHERE " + dbHelper.FDAMHED_REFNO+ " = '" +fdamhed.getFDAMHED_REFNO()+ "'";
			cursor = dB.rawQuery(selectQuery, null);
			
			//values.put(dbHelper.FTRANHED_ID, ftranhed.getFTRANHED_ID());
			values.put(dbHelper.FDAMHED_REFNO, fdamhed.getFDAMHED_REFNO());
			values.put(dbHelper.FDAMHED_TXNDATE, fdamhed.getFDAMHED_TXNDATE());
			values.put(dbHelper.FDAMHED_MANUREF, fdamhed.getFDAMHED_MANUREF());
			values.put(dbHelper.FDAMHED_COSTCODE, fdamhed.getFDAMHED_COSTCODE());
			values.put(dbHelper.FDAMHED_REMARKS, fdamhed.getFDAMHED_REMARKS());
			values.put(dbHelper.FDAMHED_TXNTYPE, fdamhed.getFDAMHED_TXNTYPE());
			values.put(dbHelper.FDAMHED_TOTALAMT, fdamhed.getFDAMHED_TOTALAMT());
			values.put(dbHelper.FDAMHED_REACODE, fdamhed.getFDAMHED_REACODE());
			values.put(dbHelper.FDAMHED_GLPOST, fdamhed.getFDAMHED_GLPOST());
			values.put(dbHelper.FDAMHED_GLBATCH, fdamhed.getFDAMHED_GLBATCH());
			values.put(dbHelper.FDAMHED_ADDUSER, fdamhed.getFDAMHED_ADDUSER());
			values.put(dbHelper.FDAMHED_ADDDATE, fdamhed.getFDAMHED_ADDDATE());
			values.put(dbHelper.FDAMHED_ADDMACH, fdamhed.getFDAMHED_ADDMACH());
			values.put(dbHelper.FDAMHED_DEALCODE, fdamhed.getFDAMHED_DEALCODE());
			values.put(dbHelper.FDAMHED_LOCFROM, fdamhed.getFDAMHED_LOCFROM());
			values.put(dbHelper.FDAMHED_LOCTO, fdamhed.getFDAMHED_LOCTO());
			values.put(dbHelper.FDAMHED_IS_SYNCED, fdamhed.getFDAMHED_IS_SYNCED());
			values.put(dbHelper.FDAMHED_ACTIVE_STATE, fdamhed.getFDAMHED_ACTIVE_STATE());
			
				
			int count = cursor.getCount();
			if(count>0)
			{
				serverdbID = dB.update(dbHelper.TABLE_FDAMHED, values,dbHelper.FDAMHED_REFNO + " =?",	
						new String[] { String.valueOf(fdamhed.getFDAMHED_REFNO())});
				//serverdbID = dB.update(table, values, whereClause, whereArgs)
			}else
			{
				serverdbID = (int) dB.insert(dbHelper.TABLE_FDAMHED, null, values);
			}
			
			}
		}catch (Exception e) {
		
			Log.v("Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return serverdbID;
				
	}
	public ArrayList<String> gettranhed(String RefNo)
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Itemname = new ArrayList<String>();

		String selectQuery = "select * from FDamHed where RefNo = '"+RefNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			Itemname.add(cursor.getString(0));
			
			
			
		}
		
		return Itemname;
	}
	
	public ArrayList<FDamHed> getAllActiveFtranHed(String refno) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<FDamHed> list = new ArrayList<FDamHed>();
		
		
		@SuppressWarnings("static-access")
		String selectQuery = "select * from FDamHed where RefNo = '"+refno+"'";
		
	
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){
			
			FDamHed fdamhed =new FDamHed();
		
			fdamhed.setFDAMHED_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_ID)));
			fdamhed.setFDAMHED_REFNO(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_REFNO)));
			fdamhed.setFDAMHED_TXNDATE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_TXNDATE)));
			fdamhed.setFDAMHED_MANUREF(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_MANUREF)));
			fdamhed.setFDAMHED_COSTCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_COSTCODE)));
			fdamhed.setFDAMHED_REMARKS(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_REMARKS)));
			fdamhed.setFDAMHED_TXNTYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_TXNTYPE)));
			fdamhed.setFDAMHED_TOTALAMT(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_TOTALAMT)));
			fdamhed.setFDAMHED_DEALCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_DEALCODE)));
			fdamhed.setFDAMHED_ITEMTYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_ITEMTYPE)));
			fdamhed.setFDAMHED_REACODE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_REACODE)));
			fdamhed.setFDAMHED_GLPOST(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_GLPOST)));
			fdamhed.setFDAMHED_GLBATCH(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_GLBATCH)));
			fdamhed.setFDAMHED_ADDUSER(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_ADDUSER)));
			fdamhed.setFDAMHED_ADDDATE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_ADDDATE)));
			fdamhed.setFDAMHED_ADDMACH(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_ADDMACH)));
			fdamhed.setFDAMHED_LOCFROM(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_LOCFROM)));
			fdamhed.setFDAMHED_LOCTO(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_LOCTO)));
			
			
			list.add(fdamhed);
			
		}
		
		return list;
	}

	
	
	
	public ArrayList<LoadingMapper> getAllUnSyncTranHed() {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<LoadingMapper> list = new ArrayList<LoadingMapper>();
		
		
		@SuppressWarnings("static-access")
		String selectQuery = "select * from FTranHed where issync='0'";
		
	
		Cursor cursor = dB.rawQuery(selectQuery, null);
		localSP = context.getSharedPreferences(SETTINGS,Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
		
		while(cursor.moveToNext()){
			
			LoadingMapper loadingMapper =new LoadingMapper();
			OrdDetDS detDS =new OrdDetDS(context);
			SalRepDS repDS=new SalRepDS(context);
			
			CompanyBranchDS branchDS=new CompanyBranchDS(context);
			loadingMapper.setNextNumVal(branchDS.getCurrentNextNumVal(context.getResources().getString(R.string.LodNumVal)));
			
			loadingMapper.setDistDB(localSP.getString("Dist_DB", "").toString());
			loadingMapper.setConsoleDB(localSP.getString("Console_DB", "").toString());
			loadingMapper.setSALEREP_DEALCODE(repDS.getDealCode());
			loadingMapper.setSALEREP_AREACODE(repDS.getAreaCode());
			loadingMapper.setFTRANHED_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_ID)));
			loadingMapper.setFTRANHED_REFNO(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_REFNO)));
			loadingMapper.setFTRANHED_TXNDATE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_TXNDATE)));
			loadingMapper.setFTRANHED_MANUREF(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_MANUREF)));
			loadingMapper.setFTRANHED_COSTCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_COSTCODE)));
			loadingMapper.setFTRANHED_REMARKS(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_REMARKS)));
			loadingMapper.setFTRANHED_TXNTYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_TXNTYPE)));
			loadingMapper.setFTRANHED_TOTALAMT(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_TOTALAMT)));
			loadingMapper.setFTRANHED_DELPERSN(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_DELPERSN)));
			loadingMapper.setFTRANHED_DELADD1(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_DELADD1)));
			loadingMapper.setFTRANHED_DELADD2(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_DELADD2)));
			loadingMapper.setFTRANHED_DELADD3(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_DELADD3)));
			loadingMapper.setFTRANHED_VEHICALNO(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_VEHICALNO)));
			loadingMapper.setFTRANHED_PRTCOPY(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_PRTCOPY)));
			loadingMapper.setFTRANHED_GLPOST(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_GLPOST)));
			loadingMapper.setFTRANHED_GLBATCH(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_GLBATCH)));
			loadingMapper.setFTRANHED_ADDUSER(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_ADDUSER)));
			loadingMapper.setFTRANHED_ADDDATE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_ADDDATE)));
			loadingMapper.setFTRANHED_ADDMACH(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_ADDMACH)));
			loadingMapper.setFTRANHED_DEALCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_DEALCODE)));
			loadingMapper.setFTRANHED_LONGITUDE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_LONGITUDE)));
			loadingMapper.setFTRANHED_LATITUDE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_LATITUDE)));
			loadingMapper.setFTRANHED_LOCFROM(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_LOCFROM)));
			loadingMapper.setFTRANHED_LOCTO(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_LOCTO)));
			
			FTranDetDS fTranDetDS=new FTranDetDS(context);
			loadingMapper.setfTranDet(fTranDetDS.getAllloadDetails(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_REFNO))));
			
			list.add(loadingMapper);
			
		}
		
		return list;
	}
	
	@SuppressWarnings("static-access")
	public int updateIsSynced(LoadingMapper mapper) {
		
	int count =0;
		
	if(dB == null){
		open();
	}else if(!dB.isOpen()){
		open();
	}
	Cursor cursor = null;
		
	try{
		ContentValues values = new ContentValues();

		values.put(dbHelper.FTRANHED_IS_SYNCED,"1");
		
		if(mapper.isIS_SYNCED()){
			count = dB.update(dbHelper.TABLE_FTRANHED, values,dbHelper.FTRANHED_REFNO + " =?",	
							new String[] { String.valueOf(mapper.getFTRANHED_REFNO())});
		}
		
	}catch (Exception e) {
		
		Log.v(TAG+ " Exception", e.toString());
	

	}finally {  
		if (cursor !=null) {
			cursor.close();
			}
		dB.close();
		}
	return count;
				
}
	
	
	@SuppressWarnings("static-access")
	public int Finalupdate(ArrayList<FDamHed>  list) 
	{
		
		int serverdbID =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		
			
			
    for (FDamHed fdamhed : list) 
    {
		
			ContentValues values = new ContentValues();
			
			String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FDAMHED
					+ " WHERE " + dbHelper.FDAMHED_REFNO+ " = '" +fdamhed.getFDAMHED_REFNO()+ "'";
			cursor = dB.rawQuery(selectQuery, null);
			
			//values.put(dbHelper.FTRANHED_ID, ftranhed.getFTRANHED_ID());
			values.put(dbHelper.FDAMHED_REFNO, fdamhed.getFDAMHED_REFNO());
//			values.put(dbHelper.FTRANHED_TXNDATE, ftranhed.getFTRANHED_TXNDATE());
//			values.put(dbHelper.FTRANHED_MANUREF, ftranhed.getFTRANHED_MANUREF());
//			values.put(dbHelper.FTRANHED_COSTCODE, ftranhed.getFTRANHED_COSTCODE());
//			values.put(dbHelper.FTRANHED_REMARKS, ftranhed.getFTRANHED_REMARKS());
//			values.put(dbHelper.FTRANHED_TXNTYPE, ftranhed.getFTRANHED_TXNTYPE());
			values.put(dbHelper.FDAMHED_TOTALAMT, fdamhed.getFDAMHED_TOTALAMT());
//			values.put(dbHelper.FTRANHED_DELPERSN, ftranhed.getFTRANHED_DELPERSN());
//			values.put(dbHelper.FTRANHED_DELADD1, ftranhed.getFTRANHED_DELADD1());
//			values.put(dbHelper.FTRANHED_DELADD2, ftranhed.getFTRANHED_DELADD2());
//			values.put(dbHelper.FTRANHED_DELADD3, ftranhed.getFTRANHED_DELADD3());
//			values.put(dbHelper.FTRANHED_VEHICALNO, ftranhed.getFTRANHED_VEHICALNO());
//			values.put(dbHelper.FTRANHED_PRTCOPY, ftranhed.getFTRANHED_PRTCOPY());
//			values.put(dbHelper.FTRANHED_GLPOST, ftranhed.getFTRANHED_GLPOST());
//			values.put(dbHelper.FTRANHED_GLBATCH, ftranhed.getFTRANHED_GLBATCH());
//			values.put(dbHelper.FTRANHED_ADDUSER, ftranhed.getFTRANHED_ADDUSER());
//			values.put(dbHelper.FTRANHED_DEALCODE, ftranhed.getFTRANHED_DEALCODE());
			
			
//			values.put(dbHelper.FTRANHED_LOCFROM, ftranhed.getFTRANHED_LOCFROM());
//			values.put(dbHelper.FTRANHED_LOCTO, ftranhed.getFTRANHED_LOCTO());
			values.put(dbHelper.FDAMHED_IS_SYNCED, fdamhed.getFDAMHED_IS_SYNCED());
			values.put(dbHelper.FDAMHED_ACTIVE_STATE, fdamhed.getFDAMHED_ACTIVE_STATE());
			
		
			
			int count = cursor.getCount();
			if(count>0)
			{
				serverdbID = dB.update(dbHelper.TABLE_FDAMHED, values,dbHelper.FDAMHED_REFNO + " =?",	
						new String[] { String.valueOf(fdamhed.getFDAMHED_REFNO())});
				//serverdbID = dB.update(table, values, whereClause, whereArgs)
			}else
			{
				serverdbID = (int) dB.insert(dbHelper.TABLE_FDAMHED, null, values);
			}
			
			}
		}catch (Exception e) {
		
			Log.v("Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return serverdbID;
				
	}
	
	@SuppressWarnings("static-access")
	public int DeltranHed(String refno) {
		
	int count =0;
		
	if(dB == null){
		open();
	}else if(!dB.isOpen()){
		open();
	}
	Cursor cursor = null;
		
	try{
					
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FDAMHED + " WHERE " + dbHelper.FDAMHED_REFNO + " = '" +refno + "'";	
		cursor = dB.rawQuery(selectQuery, null);
		int cn = cursor.getCount();
			
		if(cn>0){
			int success = dB.delete(dbHelper.TABLE_FDAMHED, dbHelper.FDAMHED_REFNO + " ='"+refno+"'", null);
			Log.v("Success", success+"");
		}
				
		
	}catch (Exception e) {
		Log.v(TAG+ " Exception", e.toString());
	}
		finally {  
			if (cursor !=null) {
				cursor.close();
				}
			dB.close();
		}
	return count;
				
}
	
	public ArrayList<FDamHed> getAllHedData() {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<FDamHed> list = new ArrayList<FDamHed>();
		
		
		@SuppressWarnings("static-access")
		String selectQuery = "select * from FDamHed where txntype='42'";
		
	
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){
			
			FDamHed fdamhed =new FDamHed();
		
			fdamhed.setFDAMHED_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_ID)));
			fdamhed.setFDAMHED_REFNO(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_REFNO)));
			fdamhed.setFDAMHED_TXNDATE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_TXNDATE)));
			fdamhed.setFDAMHED_MANUREF(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_MANUREF)));
			fdamhed.setFDAMHED_COSTCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_COSTCODE)));
			fdamhed.setFDAMHED_REMARKS(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_REMARKS)));
			fdamhed.setFDAMHED_TXNTYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_TXNTYPE)));
			fdamhed.setFDAMHED_TOTALAMT(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_TOTALAMT)));
			fdamhed.setFDAMHED_DEALCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_DEALCODE)));
			fdamhed.setFDAMHED_GLPOST(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_GLPOST)));
			fdamhed.setFDAMHED_GLBATCH(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_GLBATCH)));
			fdamhed.setFDAMHED_ADDUSER(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_ADDUSER)));
			fdamhed.setFDAMHED_ADDDATE(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_ADDDATE)));
			fdamhed.setFDAMHED_ADDMACH(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_ADDMACH)));
			
			fdamhed.setFDAMHED_LOCFROM(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_LOCFROM)));
			fdamhed.setFDAMHED_LOCTO(cursor.getString(cursor.getColumnIndex(dbHelper.FDAMHED_LOCTO)));
			

			
			
			list.add(fdamhed);
			
		}
		
		return list;
	}
	
	
	
	
	@SuppressWarnings("static-access")
	public int restData(String refno) {
		
	int count =0;
		
	if(dB == null){
		open();
	}else if(!dB.isOpen()){
		open();
	}
	Cursor cursor = null;
		
	try{
					
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FDAMHED + " WHERE " + dbHelper.FDAMHED_REFNO + " = '" +refno + "'";	
		cursor = dB.rawQuery(selectQuery, null);
		int cn = cursor.getCount();
			
		if(cn>0){
			int success = dB.delete(dbHelper.TABLE_FDAMHED, dbHelper.FDAMHED_REFNO + " ='"+refno+"'", null);
			Log.v("Success", success+"");
		}
				
		
	}catch (Exception e) {
		Log.v(TAG+ " Exception", e.toString());
	}
		finally {  
			if (cursor !=null) {
				cursor.close();
				}
			dB.close();
		}
	return count;
				
}
	public String getLocCodebyRefNo(String refno) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		
		
		String details = null;
		@SuppressWarnings("static-access")
		String selectQuery = "select * from FDamhed where RefNo = '"+refno+"'";
		
	
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){
			
			FDamHed fdamhed =new FDamHed();


			details = cursor.getString(17);

			
		}
		
		return details;
	}
	
	@SuppressWarnings("static-access")
	public String getLocCodeByrefUnLoad(String refno)
	{
		
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		String selectQuery = "select LOCFROM from fdamhed where refno = '"+refno+"'" ;
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(0);
			
		}
			
		return "";
	}
	
}
